import 'package:chat/model/message_model.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class GroupChatController extends GetxController {
  final RxList<Message> messages = <Message>[].obs;
  final RxBool showOptions = false.obs;
  final RxBool showImg = false.obs;
  final FocusNode textFieldFocusNode = FocusNode();
  final RxString currentContact = '物业管理群'.obs;
  final TextEditingController messageController = TextEditingController();
  final List<String> emojis = [
    '😀',
    '😃',
    '😄',
    '😁',
    '😆',
    '😅',
    '😂',
    '🤣',
    '😊',
    '😇',
    '🙂',
    '🙃',
    '😉',
    '😌',
    '😍',
    '🥰',
    '😘',
    '😗',
    '😙',
    '😚',
    '😋',
    '😛',
    '😝',
    '😜',
    '🤪',
    '🤨',
    '🧐',
    '🤓',
    '😎',
    '🤩',
    '🥳',
    '😏',
  ];

  @override
  void onInit() {
    super.onInit();
    loadMessages();
    // 监听焦点变化
    textFieldFocusNode.addListener(() {
      if (textFieldFocusNode.hasFocus) {
        // 当输入框获得焦点时，强制关闭其他面板
        showImg.value = false;
        showOptions.value = false;
      }
    });
  }

  @override
  void onClose() {
    messageController.dispose();
    super.onClose();
  }

  void loadMessages() {
    messages.assignAll([
      Message(
        id: '1',
        senderId: '1',
        senderName: 'seth.eth',
        senderAvatar: 'https://via.placeholder.com/50',
        content: 'Bro! you have to check this collection out',
        time: DateTime.now().subtract(const Duration(minutes: 30)),
        isMe: false,
      ),
      Message(
        id: '2',
        senderId: '2',
        senderName: 'Me',
        senderAvatar: 'https://via.placeholder.com/50',
        content: 'I have but i cant buy in now...',
        time: DateTime.now().subtract(const Duration(minutes: 25)),
        isMe: true,
      ),
      Message(
        id: '3',
        senderId: '2',
        senderName: 'Me',
        senderAvatar: 'https://via.placeholder.com/50',
        content: 'Is it gonna dip?',
        time: DateTime.now().subtract(const Duration(minutes: 5)),
        isMe: true,
      ),
    ]);
  }

  void toggleImg() {
    if (showOptions.value) {
      showOptions.toggle();
    }
    showImg.toggle();
  }

  void toggleOptions() {
    if (showImg.value) {
      showImg.toggle();
    }
    showOptions.toggle();
  }

  void sendMessage() {
    if (messageController.text.trim().isEmpty) return;

    final newMessage = Message(
      id: DateTime.now().millisecondsSinceEpoch.toString(),
      senderId: '2',
      senderName: 'Me',
      senderAvatar: 'https://via.placeholder.com/50',
      content: messageController.text,
      time: DateTime.now(),
      isMe: true,
    );

    messages.add(newMessage);
    messageController.clear();

    // 模拟回复
    Future.delayed(const Duration(seconds: 1), () {
      final replyMessage = Message(
        id: DateTime.now().millisecondsSinceEpoch.toString(),
        senderId: '1',
        senderName: 'seth.eth',
        senderAvatar: 'https://via.placeholder.com/50',
        content: 'Reply to your message',
        time: DateTime.now(),
        isMe: false,
      );
      messages.add(replyMessage);
    });
  }

  void sendMedia(MessageType type) {
    // 处理发送不同类型的媒体消息
    String content;
    switch (type) {
      case MessageType.image:
        content = '[Photo]';
        break;
      case MessageType.video:
        content = '[Video]';
        break;
      case MessageType.location:
        content = '[Location]';
        break;
      case MessageType.voice:
        content = '[Voice Message]';
        break;
      default:
        content = '';
    }

    final newMessage = Message(
      id: DateTime.now().millisecondsSinceEpoch.toString(),
      senderId: '2',
      senderName: 'Me',
      senderAvatar: 'https://via.placeholder.com/50',
      content: content,
      time: DateTime.now(),
      isMe: true,
      type: type,
    );

    messages.add(newMessage);
    showOptions.value = false;
  }
}
